<template>
	<view class="centen" >
		
		<view class="centen-1">
			<view style="flex: 3;">检查人</view>
			<view style="flex: 7;" @click="selectPreson">{{checkPreson.name}}</view>
		</view>
		<view class="centen-1">
			<view style="flex: 3;">检查时间</view>
			<view style="flex: 7;display: flex;">
		         <view style="flex: 4;">{{checkTime}}</view>
				 <view style="flex: 3;text-align: right;"><test-com beginYear="2000" endYear="2030" @confirm="onConfirm"></test-com></view>
			</view>
		</view>
		<view class="centen-1">
			<view style="flex: 3;">检查项目</view>
			<view style="flex: 7;" >
				<input  v-model="checkItem" @focus="bbte" @blur="blte"
				  style="position: relative;top: 20rpx;"/>
			</view>
		</view>
		<view class="centen-1">
			<view style="flex: 3;">设备状态</view>
			<view style="flex: 7;" >
				<picker @change="bindPickerChanges" range-key="label" :data-index="22" :data-id="objectArray[index].id" :value="index" :range="objectArray">
					<view class="uni-input">{{objectArray[index].label}}</view>
					<!-- 还是建议用input保存,可能picker更新不及时 -->
					<input type="text" :value="objectArray[index].id" hidden/>
				</picker>

			</view>	
		</view>
		<textarea auto-height="60" @focus="bbtet1" @blur="bltet1" v-model="checkSituation" />
		<textarea auto-height="60"  @focus="bbtet2" @blur="bltet2" v-model="measures"/>
		
	    <view class="centimg">
			<view class="maggview">上传图片</view>
			<view class="maggfour" >
				<image src="../../static/images/add2.png"  @click="getImage"></image>
				<image :src="val + item.imgPath" v-for="item in fileImg"></image>
			</view>
			
		</view>
		
		<button @click="save">保存</button>
		
	</view>
</template>

<script>
	import testCom from '../../components/test_com.vue'
	let _this;
	
	export default {
		components: {
			testCom
		},
		data() {
			return {			
				val:"",
				value:"",
				imgList: [],
				optionitem:"",
				imgMaxNum: 9,
				checkItem:"请填写检查项目",
				equipmentState:"请选择设备状态",
				checkTime:"请选择检查时间",
				checkPreson:{
					        id:"",
							name:"请选择检查人",
							},
				checkSituation:"请输入检查情况...",
				measures:"请输入整改措施...",
				fileImg:[],  //已经选择的照片			
				index:0,
				//字典信息
				objectArray: [{  
					id: 0,
					label:"请选择设备状态", //值
					value:0   //缩写
				}],
				
				
				
			}
		},
		onLoad(option) {
			_this=this;
			console.log(option) //有项目id
			this.optionitem=option;
			
			this.val= uni.getStorageSync("pathHost");
			this.value=uni.getStorageSync("JSESSIONID");
			this.getDictionaries();
		},
		onShow() {
			_this.$eventHub.$on('fire', function (data) {
			 	//console.log('fire - '+JSON.stringify(data));
				_this.checkPreson=data;
				_this.$eventHub.$off('fire');
				
			});
			
			
		},
		methods: {
			blte(){
				if(_this.checkItem==""){
					_this.checkItem="请填写检查项目";
				}
				
			},
			bbte(){
				//console.log("dfdf")
				if(_this.checkItem=="请填写检查项目"){
					_this.checkItem="";
				}
	
			},
			bltet1(){
				if(_this.checkSituation==""){
					_this.checkSituation="请输入检查情况...";
				}
				
			},
			bbtet1(){
				//console.log("dfdf")
				if(_this.checkSituation=="请输入检查情况..."){
					_this.checkSituation="";
				}
				
			},
			bltet2(){
				if(_this.measures==""){
					_this.measures="请输入整改措施...";
				}
				
			},
			bbtet2(){
				//console.log("dfdf")
				if(_this.measures=="请输入整改措施..."){
					_this.measures="";
				}
				
			},
			//获取字典信息
			getDictionaries(){
				uni.request({
					url: _this.val+'/ggcapp/sys/dict/getDictValue'+";JSESSIONID="+_this.value+"?__ajax=true&mobileLogin=true&pageSize=999&dictTypeId=99966b6a40464ec390531d2ea193778d" ,
					success(data) {
							//console.log(data.data.rows.length)
							//console.log(data)
							for (var i = 0; i < data.data.rows.length; i++) {
							   _this.objectArray.push({
								   id: data.data.rows[i].id,
								   label:data.data.rows[i].label, //值
								   value:data.data.rows[i].value   //缩写
							   })	   
							}
					}
				});	
			},
			onConfirm(val) {
				//console.log(val)
				_this.checkTime=val.selectRes;
				
			},
			selectPreson(){
			  
				uni.navigateTo({
					url: '/pages/newCheck/selectUser?type=1'
				});
			
			},
			bindPickerChanges: function(e) {
			    this.index = e.detail.value
			   /*
			    console.log('可以传data-xx:xx',e.currentTarget.dataset.index,
				'\n默认传过来的是下标：',e.detail.value,
				'\n也可以传普通json传过来的id等：',
				e.currentTarget.dataset.id); */
				_this.equipmentState=_this.objectArray[e.detail.value];
				
			},
			//添加图片
			getImage:function(){
			console.log("dddddd")
				uni.chooseImage({
								count: 9, //默认9
								sizeType: ['original'], //可以指定是原图还是压缩图，默认二者都有
								sourceType: ['camera','album'], //从相机、相册选择
								success: (res) => {
									
									var tempFilePaths = res.tempFiles;
									// console.log(res)
									if (_this.imgList.length+tempFilePaths.length > _this.imgMaxNum) {
										uni.showToast({
											title: '超过上传图片的最大数量',
											icon: 'none'
										})
									} else {
										if (_this.imgList.length != 0) {
											_this.imgList = _this.imgList.concat(res.tempFilePaths);
										} else {
											_this.imgList = res.tempFilePaths;
										}
										
										//  console.log(tempFilePaths.length)
										//  console.log(tempFilePaths)
										/* console.log(tempFilePaths.length)
										uni.showLoading({
											title: '图片上传中...'
										}); */
										for (var i = 0; i < tempFilePaths.length; i++) {
											// console.log(tempFilePaths[i])
											 uni.uploadFile({
												url: _this.val + "/ggcapp/sys/file/webupload/upload"+";JSESSIONID="+_this.value+"?__ajax=true&mobileLogin=true&uploadPath=/appFile/",
												filePath: tempFilePaths[i].path,
												name: "file", // 一定要与后台@RequestParam("file") MultipartFile变量名一致
												success(res) {
												//	console.log(res);
													let obj=JSON.parse(res.data);
													// console.log(obj);
													if(obj.success){
														_this.fileImg.push({
															imgPath: obj.body.url
														})
													}
													
												}
											}); 
										}
										//	uni.hideLoading();
									}
								}
							});
			},
			save(){				
				let imlist=""
				for (var i = 0; i < _this.fileImg.length; i++) {
					imlist=imlist+_this.fileImg[i].imgPath+"|";
				}
				if(imlist!=null && imlist!=""){
					imlist = imlist.substring(0,imlist.lastIndexOf('|'));
				}
				uni.request({
					url: _this.val+'/ggcapp/equipmentmanage/equipmentcheck/ggEquipmentCheck/save'+";JSESSIONID="+_this.value+"?__ajax=true&mobileLogin=true" ,
					data:{
						checkPreson:_this.checkPreson.id,
						checkTime:_this.checkTime,
						checkItem:_this.checkItem,
						checkSituation:_this.checkSituation,
						equipmentState:_this.equipmentState.value,
						measures:_this.measures,
						file:imlist,
						equipmentId:_this.optionitem.eqid  //项目id
					},
					success(data) {
							//console.log(data)
							if(data.data.success){
								uni.showToast({
									title: '添加成功',
									icon: "none",
									mask: true,
									duration: 1600
								}); 
								setTimeout(function(){
									uni.navigateBack({
									    delta: 1,					
									    animationDuration: 400
									});
								},1600); 
								
							}
					}
				});
				
				
				
				
				
				
				
			}
			
		}
	}
</script>

<style>
	page{
		background-color: white;
	}
   .centen{
	   background-color: white;
	   padding: 0rpx 50rpx 0rpx 50rpx;
	   
	   
   }
   .centen-1{
	   display: flex;
	   flex: 10;
	   height: 80rpx;
	   line-height: 80rpx;
        border-bottom: 1rpx solid #DEE4E4;
   }
   textarea{
	   min-height: 160rpx;
	   background-color: rgb(245, 245, 249);
	   width: 100%;
	   margin-bottom: 20rpx;
	   
   }
   .centimg{
	   min-height: 180rpx;
	   margin-bottom: 10rpx;
   }
   button{
	   background-color: #0081FF;
	   color: white;
   }

.maggfour{
		background-color: white;
		min-height: 200rpx;
		padding: 20rpx 0rpx 20rpx 0rpx;
		overflow-y: auto;
		
	}
	.maggfour>image{
		float: left;
	    width: 120rpx;
		height: 120rpx;
		border-radius: 4rpx;
		margin: 10rpx;
	}


</style>
